use std::collections::btree_map::Range;
use std::io;
use std::time::{Instant, SystemTime}; // 引入标准库

//    crate::guess_number::guess();
//    crate::collection::hash_map();

fn main() {
    println!("{:?}", IpAddrKind::V4)
}

fn area(width: u32, height: u32) -> u32 {
    width * height
}

fn area1(dimensions: (u32, u32)) -> u32 {
    dimensions.0 * dimensions.1
}

fn area2(rectangle: &Rectangle) -> u32 {
    rectangle.width * rectangle.height
}

#[derive(Debug)]
enum IpAddrKind {
    V4,
    V6,
}

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }

    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }

    fn square(size: u32) -> Rectangle {
        Rectangle {
            width: size,
            height: size,
        }
    }
}

struct Color(i32, i32, i32);

struct Point(i32, i32, i32);

#[derive(Debug)]
struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

fn build_user(email: String, username: String) -> User {
    User {
        username,
        email: email,
        sign_in_count: 1,
        active: true,
    }
}

fn first_word(s: &str) -> &str {
    let bytes = s.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[..i];
        }
    }
    &s[..]
}

// 将华氏温度转换为摄氏温度
fn convert_f_2_c(f: f32) -> f32 {
    5.0 / 9.0 * (f - 32.0)
}

// 将摄氏温度转换为华氏温度
fn convert_c_2_f(c: f32) -> f32 {
    9.0 / 5.0 * c + 32.0
}

//生成斐波那契数列
fn generate_fibonacci_sequence(n: i32) -> i32 {
    if n < 1 {
        0
    } else if n < 3 {
        1
    } else {
        generate_fibonacci_sequence(n - 1) + generate_fibonacci_sequence(n - 2)
    }
}

fn print_lyric() {
    let day = [
        "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
        "tenth", "eleventh", "twelfth",
    ];
    let gift: [&str; 12] = [
        "And A partridge in a pear tree",
        "Two turtle doves, ",
        "Three French hens, ",
        "Four calling birds, ",
        "Five golden rings, ",
        "Six geese a-laying, ",
        "Seven swans a-swimming, ",
        "Eight maids a-milking, ",
        "Nine ladies dancing, ",
        "Ten lords a-leaping, ",
        "Eleven pipers piping, ",
        "Twelve drummers drumming, ",
    ];
    let mut gift2 = [""; 12];
    for i in 1..gift.len() {
        // todo 怎么连接两个str
        //        let string: str = gift[i] + &gift2[i - 1];
        //        gift2[i] = &string;
    }
    //    let mut gift2 = "";
    for i in 0..day.len() {
        println!(
            "On the {} day of Christmas, my true love send to me:{}",
            i, gift2[i]
        );
    }
}

fn generate_fibonacci_sequence2(n: i32) -> i32 {
    if n < 1 {
        0
    } else if n < 3 {
        1
    } else {
        generate_fibonacci_sequence(n - 1) + generate_fibonacci_sequence(n - 2)
    }
}

fn another_function(i: i32, j: f64) -> i8 {
    5
}

fn basic_program_concepts() {
    let num = 2;
    if num < 3 {
        println!("condition was true")
    } else {
        println!("condition was false")
    }

    // 在let语句中使用if。因为if是一个表达式，所以可以在let语句的右侧使用。
    let condition = true;
    // 两个代码块的返回值类型必须相同，否则编译不通过
    let number = if condition { 5 } else { 6 };
    println!("the value of number is : {}", number);

    let mut i = 0;
    loop {
        if i >= 10 {
            break;
        }
        println!("{}", i);
        i += 1;
    }

    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 10 {
            break counter * 2;
        }
    };
    assert_eq!(result, 20);

    // while结构消除了很多使用 loop、if、else 和 break 时所必须的嵌套，这样更加清晰。当条件为真就执行，否则退出循环。
    let mut c = 3;
    while c != 0 {
        println!("{}!", c);
        c -= 1;
    }
    println!("LIFTOFF!!!");

    // 遍历一个数组
    let a = [1, 2, 3, 4, 5];
    let mut index = 0;
    while index < a.len() {
        println!("{}:{}", index, a[index]);
        index += 1;
    }

    for e in a.iter() {
        println!("{}", e);
    }

    println!("倒计时开始：");
    for e in (0..11).rev() {
        println!("{}", e);
    }
}

// 猜数游戏
mod guess_number {
    use rand::Rng;
    use std::cmp::Ordering;
    use std::io;

    pub fn guess() {
        println!("Guess the number!");

        let secret_number = rand::thread_rng().gen_range(1, 101);

        loop {
            println!("Please input your guess.");

            let mut guess = String::new();

            io::stdin()
                .read_line(&mut guess)
                .expect("Faild to read line");

            // 转换为数字类型
            let guess: u32 = match guess.trim().parse() {
                Ok(num) => num,
                Err(_) => continue,
            };

            println!("Your guessed: {}", guess);
            match guess.cmp(&secret_number) {
                Ordering::Less => println!("Too small!"),
                Ordering::Equal => {
                    println!("You win!");
                    break;
                }
                Ordering::Greater => println!("Too big!"),
            }
        }
    }
}

mod collection {
    use std::collections::HashMap;

    pub fn hash_map() {
        let field_name = String::from("Favorite color");
        let field_value = String::from("Blue");
        let mut map = HashMap::new();
        map.insert(field_name, field_value);
    }
}
